package com.wlcorp.forcazamis.web.action.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wlcorp.forcazamis.business.system.IRoleService;
import com.wlcorp.forcazamis.domain.system.Role;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.WebUtil;

public class RoleAction extends DispatchAction{
	
	public ActionForward getRoleList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		List<Role> roleList = ContextUtil.getInstance().getRoleService().getRoleList(null);
		request.setAttribute("roleList",roleList);
		return mapping.findForward("list");
	}
	
	public ActionForward getRole(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Role role = new Role();
		WebUtil.setRequestToObject(request,role);
		role = ContextUtil.getInstance().getRoleService().getRoleById(role.getRoleId());
		if(null == role){
			role = new Role();
		}
		request.setAttribute("role",role);
		return mapping.findForward("modify");
	}
	
	public ActionForward modifyRole(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Role role = new Role();
		WebUtil.setRequestToObject(request,role);
		ContextUtil.getInstance().getRoleService().saveRole(role);
		WebUtil.setAlertMessage(request,"操作成功！");
		request.setAttribute("role",role);
		return mapping.findForward("modify");
	}
	public ActionForward deleteRole(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Role role = new Role();
		WebUtil.setRequestToObject(request,role);
		IRoleService service = ContextUtil.getInstance().getRoleService();
		role = service.getRoleById(role.getRoleId());
		if(null != role){
			service.deleteRole(role);
			if(AppConstant.BASE_BEAN_STATUS_ERROR == role.getStatus()){
				WebUtil.setAlertMessage(request,role.getMsg());
			}
		}
		return getRoleList(mapping,form,request,response);
	}
}
